package com.woniuxy.oldmanapi.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Outapplication;
import com.woniuxy.oldmanapi.service.OutapplicationService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import com.woniuxy.oldmanapi.yvo.OutApplicationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
public class OutApplicationController {
    @Autowired
    OutapplicationService outapplicationService;

    /**
     * 此方法为初始化外出申请表页面数据
     *
     * @param name 搜索框条件
     * @param now  分页 页数
     * @param size 分页 条数
     * @return
     */
    @GetMapping("/outApplication/list")
    public ResponseResult<PageInfo<OutApplicationVo>> initSelectAllByCons(String name, int now, int size) {
        ResponseResult<PageInfo<OutApplicationVo>> result = new ResponseResult<>();
        try {
            PageInfo<OutApplicationVo> pageInfo = outapplicationService.initSelectAllByCons(name, now, size);
            if (pageInfo.getList() == null) {
                result.setCode(204);
                result.setMsg("无满足条件对象");
            } else {
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(pageInfo);
            }
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    /**
     * 此方法为添加外出申请
     *
     * @param outapplication 外出申请对象 非 vo
     * @return
     */
    @PostMapping("/outApplication/addOutApplication")
    public ResponseResult<Void> addOutApplication(@RequestBody Outapplication outapplication) {
/*测试*/
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            outapplicationService.addOutApplication(outapplication);
            result.setMsg("创建成功");
            result.setCode(200);
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    /**
     * 此方法用于 打开编辑修改时 查询一个对象 返显使用
     *
     * @param id 查询对象的Id
     * @return
     */

    @GetMapping("/outApplication/selectOutApplication/{id}")
    public ResponseResult<OutApplicationVo> selectOutApplicationById(@PathVariable("id") int id) {
        ResponseResult<OutApplicationVo> result = new ResponseResult<>();
        try {
            OutApplicationVo outApplicationVo = outapplicationService.selectOutApplicationById(id);
            if (outApplicationVo == null) {
                result.setCode(202);
                result.setMsg("此对象信息有误,请稍后再试");
            } else {
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(outApplicationVo);
            }
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    /**
     * 此方位用于 修改一个外出申请记录
     *
     * @param outapplication
     * @return
     */
    @PutMapping("/outApplication/modifyOutApplication")
    public ResponseResult<Void> modifyOutApplication(@RequestBody Outapplication outapplication) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
            outapplicationService.modifyOutApplication(outapplication);
            result.setMsg("修改成功");
            result.setCode(200);
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    /**
     * 此方位用于 修改一个外出申请记录的状态 修改为返院 并计算外出总时间
     * @param id  此申请对象ID
     * @param backtime 确定的返院时间
     * @return
     */
    @PutMapping("/outApplication/modifyOutApplicationStatus/{id}/{backdatetime}")
    public ResponseResult<Void> modifyOutApplicationStatus(@PathVariable("id") int id, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @PathVariable("backdatetime") Date backtime) {
        ResponseResult<Void> result = new ResponseResult<>();
        Outapplication outapplication = new Outapplication();
        outapplication.setId(id);
        outapplication.setBackdatetime(backtime);
        try {
            outapplicationService.modifyOutApplicationStatus(outapplication);
            result.setMsg("修改返院成功");
            result.setCode(200);
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    /**
     * 此方法用于删除一个外出申请记录
     *
     * @param id
     * @return
     */
    @DeleteMapping("/outApplication/deleteOutApplication/{id}")
    public ResponseResult<Void> deleteOutApplication(@PathVariable("id") int id) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
            outapplicationService.deleteOutApplication(id);
            result.setMsg("删除成功");
            result.setCode(200);
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }
}
